

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Store;
using System.Collections.Generic;
using JTC.MiniCommerce.Web.controls;
using JTC.MiniCommerce.Localization;


namespace JTC.MiniCommerce.Web {
  public partial class myaccount : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
    
      MembershipUser membershipUser = Membership.GetUser(WebUtility.GetUserName());
      lblUserNameDisplay.Text = membershipUser.UserName;
      lblEmailDisplay.Text = membershipUser.Email;
      if(!Page.IsPostBack) {
        LoadOrders();
        LoadAddresses();
      }

      LocalizationUtility.AddLabelText(lblMyAccount);
      LocalizationUtility.AddLabelText(lblUserName);
      LocalizationUtility.AddLabelText(lblPassword);
      LocalizationUtility.AddLinkButtonText(lbChangePassword);
      LocalizationUtility.AddLabelText(lblEmail);
      LocalizationUtility.AddLinkButtonText(lbChangeEmail);
      LocalizationUtility.AddPanelText(pnlBillingAddresses);
      LocalizationUtility.AddPanelText(pnlShippingAddresses);
      btnEmailCancel.Text = LocalizationUtility.GetText("lblCancel");
      btnEmailSave.Text = LocalizationUtility.GetText("btnSave");
      lblPasswordChange.Text = LocalizationUtility.GetText("lblPassword");
      lblEmailChange.Text = LocalizationUtility.GetText("lblEmail");
      lblEmailConfirmation.Text = LocalizationUtility.GetText("lblEmailConfirmation");
      cvEmailAddresses.ErrorMessage = LocalizationUtility.GetText("lblEmailAddressesDoNotMatch");
      Page.Title = string.Format("{0} :: {1}", Master.SiteSettings.SiteName, LocalizationUtility.GetText("lblMyAccount"));
    }

    private void LoadAddresses() {
      AddressCollection billingAddresses = LoadAddresses(AddressType.BillingAddress);
      rptrBillingAddresses.DataSource = billingAddresses;
      rptrBillingAddresses.DataBind();

      AddressCollection shippingAddresses = LoadAddresses(AddressType.ShippingAddress);
      rptrShippingAddresses.DataSource = shippingAddresses;
      rptrShippingAddresses.ItemDataBound += new RepeaterItemEventHandler(rptrShippingAddresses_ItemDataBound);
      rptrShippingAddresses.DataBind();
    }

    void rptrShippingAddresses_ItemDataBound(object sender, RepeaterItemEventArgs e) {
     addressdisplay add = e.Item.FindControl("dcShippingAddress") as addressdisplay;
     if(add != null) {
      add.Address = e.Item.DataItem as Address;
     }
    }

    private AddressCollection LoadAddresses(AddressType addressType) {
      List<Address> addressList = WebProfile.Current.AddressCollection.FindAll(delegate(Address address) {
        return address.AddressType == addressType;
      });
      AddressCollection addressCollection = new AddressCollection();
      addressCollection.AddRange(addressList);
      return addressCollection;
    }

    private void LoadOrders() {
      OrderCollection orderCollection = new OrderController().FetchOrdersForUser(WebUtility.GetUserName());
      dgOrders.DataSource = orderCollection;
      dgOrders.ItemDataBound += new DataGridItemEventHandler(dgOrders_ItemDataBound);
      dgOrders.Columns[0].HeaderText = LocalizationUtility.GetText("hdrDetails");
      dgOrders.Columns[1].HeaderText = LocalizationUtility.GetText("hdrOrderNumber");
      dgOrders.Columns[2].HeaderText = LocalizationUtility.GetText("hdrTotal");
      dgOrders.Columns[3].HeaderText = LocalizationUtility.GetText("hdrStatus");
      dgOrders.DataBind();
    }

    void dgOrders_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        LinkButton lbView = e.Item.FindControl("lbView") as LinkButton;
        if(lbView != null) {
          lbView.Text = LocalizationUtility.GetText("lblDetails");
        }
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }

    }
    
    
    protected void lbChangePassword_Click(object sender, EventArgs e) {
      pnlUser.Visible = false;
      pnlMyOrders.Visible = false;
      pnlChangePassword.Visible = true;
    }
    
    protected void lbChangeEmail_Click(object sender, EventArgs e) {
      pnlUser.Visible = false;
      pnlMyOrders.Visible = false;
      pnlChangeEmail.Visible = true;
    }
    
    protected void btnEmailCancel_Click(object sender, EventArgs e) {
      pnlUser.Visible = true;
      pnlMyOrders.Visible = true;
      pnlChangeEmail.Visible = false;
    }

    protected void HideChangePassword(object sender, EventArgs e) {
      pnlUser.Visible = true;
      pnlMyOrders.Visible = true;
      pnlChangePassword.Visible = false;
    }
    
    protected void btnEmailSave_Click(object sender, EventArgs e) {
      if(Membership.ValidateUser(WebUtility.GetUserName(), txtPassword.Text)) {
        MembershipUser membershipUser = Membership.GetUser(WebUtility.GetUserName());
        membershipUser.Email = txtEmail.Text;
        Membership.UpdateUser(membershipUser);
        Response.Redirect("myaccount.aspx", true);
      }
    }
    
    protected void lbView_Click(object sender, CommandEventArgs e ) {
      Response.Redirect(string.Format("receipt.aspx?oid={0}", e.CommandArgument.ToString()), true);
    }
    
    protected string GetFormattedAmount(string amount) {
      return StoreUtility.GetFormattedAmount(amount, true);
    }
    
    
  }
}
